package emil.poker;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import emil.poker.players.Player;
import emil.poker.util.CycleVector;

public class Table {
	
	private Deck deck;
	private CycleVector<Player> players;
	private int potSize;
	private HashMap<List<Player>, Double> playersCompetingForPots;
	
	public void setDealer(Player player){
		
	}
	
	public int getDealerPosition(){
		for (Player player : getPlayers()) {
			if (player.isDealer()) {
				return players.indexOf( player );
			}
		}
		return 0;
	}
	
	public int getPotSize() {
		return potSize;
	}

	public void setPotSize(int potSize) {
		this.potSize = potSize;
	}

	public void clearCommunityCards(){
		
		if ( communityCards != null ) {
			communityCards.clear();
		}
	}
	
	public void returnCommunityCardsToDeck(){
		
		if ( communityCards != null ) {
			for (Card card : communityCards) {
				deck.getCards().add( card );
			}
			communityCards.clear();
		}
	}
	
	public void clearHands(){
		if ( getPlayers() != null ) {
			
			for (Player player : getPlayers()) {
				player.clearHand();
			}
		}
	}
	
	
	
	public void collectPots(){
		
	}
	
	public void clearHandsExceptFirst(){
		boolean isFirst = true;
		for (Player player : getPlayers()) {
			if ( isFirst ) {
				isFirst = false;
			}else{
				player.clearHand();
			}
		}
	}
	
	public void clearHandsExcept( List<Player> excludePlayers ){
		for (Player player : getPlayers()) {
			if ( excludePlayers.indexOf( player ) < 0 ) {
				player.clearHand();
			}
		}
	}
	
	public void clearHandsExcept( Player excludePlayer ){
		for (Player player : getPlayers()) {
			if ( !player.equals( excludePlayer ) ) {
				player.clearHand();
			}
		}
	}
	
	public CycleVector<Player> getPlayers() {
		return players;
	}
	private List<Card> communityCards;
	
	public void setPlayers(Vector<Player> players) {
		CycleVector<Player> cycleVector = new CycleVector<Player>();
		cycleVector.addAll( players );
		this.players = cycleVector;
	}
	
	public Table() {
		communityCards = new LinkedList<Card>();
		deck = new Deck();
	}
	
	public Deck getDeck() {
		return deck;
	}
	
	/**
	 * 
	 * @return A secure copy of the list
	 */
	public List<Card> getCommunityCards() {
		return communityCards;
	}
	
	public void setCommunityCards(List<Card> communityCards) {
		this.communityCards = new LinkedList<Card>();
		this.communityCards.addAll( communityCards );
	}
	
	public void addCommunityCard(Card communityCard) {
		if (communityCards == null) {
			communityCards = new LinkedList<Card>();
			
		}
		communityCards.add( communityCard );
	}
	
	@Override
	public String toString(){
		if ( communityCards != null ) {
			return communityCards.toString();
		} else {
			return super.toString();
		}
	}

	public void setDeck(Deck deck) {
		this.deck = deck;
	}
}
